package J5_13;

import java.util.*;

public class test {

    //给定一个非空的正整数数组 nums ，请判断能否将这些数字分成元素和相等的两部分。
    public boolean canPartition(int[] arr) {
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            sum += arr[i];
        }
        if (sum % 2 == 1) {
            return false;
        }
        Arrays.sort(arr);
        int target = sum / 2, cur = 0;
        boolean[][] dp = new boolean[arr.length][target + 1];

        for (int i = 0; i < arr.length; i++) {
            dp[i][0] = true;
        }
        dp[0][arr[0]] = true;
        for (int i = 1; i < arr.length; i++) {
            for (int j = 1; j <= target; j++) {
                if (j >= arr[i]) {
                    dp[i][j] = dp[i-1][j] || dp[i-1][j-arr[i]];
                } else {
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
        return dp[arr.length-1][target];
    }

    public static void main2(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        int sum = 0;
        for (int i = 0; i < arr.length; i++) {
            arr[i] = in.nextInt();
            sum += arr[i];
        }
        if (sum % 2 == 1) {
            System.out.println("false");
            return;
        }
        Arrays.sort(arr);
        int target = sum / 2, cur = 0;
        boolean[][] dp = new boolean[arr.length][target + 1];
        for (int i = 0; i < arr.length; i++) {
            dp[i][0] = true;
        }
        dp[0][arr[0]] = true;
        for (int i = 1; i < arr.length; i++) {
            for (int j = 1; j <= target; j++) {
                if (j >= arr[i]) {
                    dp[i][j] = dp[i-1][j] || dp[i-1][j-arr[i]];
                } else {
                    dp[i][j] = dp[i-1][j];
                }
            }
        }
        System.out.println(dp[arr.length - 1][target]);

    }
    public static void main1(String[] args) {
        Scanner in = new Scanner(System.in);
        String str = in.next();
        int n = str.length();
        boolean[][] dp = new boolean[n][n];
        int maxLen = 1;
        for (int i = n - 1; i >= 0; i--) {
            for (int j = i; j < n; j++) {
                if (str.charAt(i) ==  str.charAt(j)) {
                    if (j - i < 2) {
                        dp[i][j] = true;
                    } else {
                        dp[i][j] = dp[i+1][j - 1];
                    }
                } else {
                    dp[i][j] = false;
                }
                if (dp[i][j] && maxLen< j -i + 1) {
                    maxLen = j - i + 1;
                }
            }
        }
        if (maxLen == 1) {
            System.out.println(-1);
        } else {
            System.out.println(maxLen);
        }

    }

    public static void main3(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] arr = new int[n];
        for (int i = 0; i < n; i++) {
            arr[i] = in.nextInt();
        }
        int[] dp = new int[n + 1];
        dp[1] = arr[0];
        for (int i = 2; i <= n; i++) {
            dp[i] = Math.max(dp[i-1], dp[i-2] + arr[i-1]);
        }
        System.out.println(dp[n]);
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt(), p = in.nextInt();
        TreeMap<Integer, Integer> map = new TreeMap<>();
        int max = 0;
        for (int i = 0; i < n; i++) {
            int a = in.nextInt();
            max = Math.max(max, a);
            map.put(a, map.getOrDefault(a, 0) + 1);
        }
        int l = 0, r = l + 2 * p;
        int[] arr = new int[max+1];
        for (Map.Entry<Integer, Integer> entry : map.entrySet()) {
            arr[entry.getKey()] = entry.getValue();
        }
        int sum = 0;
        for (int i = 0; i <= r; i++) {
            sum += arr[i];
        }
        int res = sum;
        for (int i = r + 1; i <= max; i++) {
            sum += arr[i];
            sum -= arr[i - 2 * p - 1];
            res = Math.max(sum ,res);
        }
        System.out.println(res);
    }
}
